﻿using System;
using System.Windows.Forms;
using BadgeManager.Controller;
using BadgeManager.Model;

namespace Badge_Manager.View
{
    public partial class frmOpzioni : Form
    {
        private Utente User;

        public frmOpzioni(Utente utente)
        {
            InitializeComponent();
            this.User = utente;
            this.ControllaPermessi();
            getDati();
        }

        private void getDati()
        {
            try
            {
                txtDurataPausa.Text = BadgeManager.Properties.Settings.Default.durataPausaPranzo.ToString();
                txtOrarioPausa.Text = BadgeManager.Properties.Settings.Default.OrarioPausaPranzo.ToString();
                txtDurataGiornataStandard.Text = BadgeManager.Properties.Settings.Default.DurataGiornataStandard.ToString();
                txtDurataCorsoStandard.Text = BadgeManager.Properties.Settings.Default.durataorecorsoStandard.ToString();
                chkConsideraSabato.Checked = BadgeManager.Properties.Settings.Default.chkConsideraSabato;
                chkConsideraDomenica.Checked = BadgeManager.Properties.Settings.Default.chkConsideraDomenica;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void setDati()
        {
            try
            {
                BadgeManager.Properties.Settings.Default.durataPausaPranzo = Convert.ToInt16(txtDurataPausa.Text);
                BadgeManager.Properties.Settings.Default.OrarioPausaPranzo = TimeSpan.Parse(txtOrarioPausa.Text);
                BadgeManager.Properties.Settings.Default.DurataGiornataStandard = Convert.ToInt16(txtDurataGiornataStandard.Text);
                BadgeManager.Properties.Settings.Default.durataorecorsoStandard = Convert.ToInt16(txtDurataCorsoStandard.Text);
                BadgeManager.Properties.Settings.Default.chkConsideraSabato = chkConsideraSabato.Checked;
                BadgeManager.Properties.Settings.Default.chkConsideraDomenica = chkConsideraDomenica.Checked;
                BadgeManager.Properties.Settings.Default.Save();
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ControllaPermessi()
        {
            if (User != null)
            {
                ctrlUtente cUtente = new ctrlUtente();
                if (!cUtente.PuoModificare(User) ||
                    !cUtente.PuoScrivere(User))
                {
                    cmdSalva.Enabled = false;
                }
            }
        }

        private void cmdAnnulla_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmdSalva_Click(object sender, EventArgs e)
        {
            setDati();
        }

        private void cmdDatabase_Click(object sender, EventArgs e)
        {
            if (!new ctrlUtente().PuoLeggere(User))
            {
                MessageBox.Show("Non hai i permessi per utilizzare questa funzione ");
                return;
            }
            frmConnessioneDB frm = new frmConnessioneDB();
            frm.Top = 20;
            frm.Left = 20;
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }
    }
}